package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.model.entity.Department;
import com.qf.model.vo.DepartmentVO;
import com.qf.service.DepartmentService;
import com.qf.util.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * (Department)表控制层
 *
 * @author ZhiHuiJun
 * @since 2024-05-02 15:03:16
 */
@RestController
@RequestMapping("/api/department")
public class DepartmentController {
    /**
     * 服务对象
     */
    @Autowired
    private DepartmentService departmentService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("/list")
    public R queryByPage(Long pageNum) {
        return R.ok(this.departmentService.queryByPage(pageNum));
    }

    @RequestMapping("/list/page")
    public R selectDeptByCondition(@RequestParam Map<String, Object> map) {
        int pageNum = Integer.parseInt((String) map.get("pageNum"));
        int pageSize = Integer.parseInt((String) map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);

        List<Department> list = departmentService.selectDeptByCondition(map);
        if (list != null && !list.isEmpty()){
            PageInfo<Department> pageInfo = new PageInfo<>(list);
            return R.ok(pageInfo);
        }
        return R.fail("查询失败");
    }

    @RequestMapping("/selectAllDept")
    public List<Department> selectAllDept() {
        return departmentService.selectAllDept();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Long id) {
        return R.ok(this.departmentService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param department 实体
     * @return 新增结果
     */
    @PostMapping("/save")
    public R add(Department department) {
        boolean result = departmentService.insert(department);
        if (result) {
            return R.ok();
        }
        return R.fail("添加失败");
    }

    /**
     * 编辑数据
     *
     * @param department 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit(Department department) {
        boolean result = departmentService.update(department);
        if (result) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/del/{id}")
    public R deleteById(@PathVariable Long id) {
        boolean result = departmentService.deleteById(id);
        if (result) {
            return R.ok();
        }
        return R.fail("删除失败");
    }

    @RequestMapping("/del/batch")
    public R deleteBatch(@RequestParam List<Integer> list) {
        boolean result = departmentService.deleteBatch(list);
        if (result) {
            return R.ok();
        }
        return R.fail("批量删除失败");
    }

    @RequestMapping("/departmentExport")
    public void departmentExport(HttpServletResponse response) {
        departmentService.departmentExport(response);
    }

    @RequestMapping("/importDepartment")
    public R importDepartment(MultipartFile file) {
        boolean result = departmentService.importDepartment(file);
        if (result){
            return R.ok();
        }
        return R.fail("文件导入失败");
    }
}

